<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品单位操作功能
 * 
 * @package 	htdocs
 * @subpackage  quanliadmin
 * @author  	allen.qiang
 */

class pa_goodsUnit {

    public function execute() {
		global $G_PGLOB ;
        if (isset($_REQUEST['operFlg'])) {

            switch($_REQUEST['operFlg']) {

                case 1: //商品单位列表
                    $this->goodsUnitList();
                    break;
                case 2: //商品单位添加
                    $this->unitAdd();
                    break;
                case 3: //商品单位编辑
                    $this->unitEdit();
                    break;
				default:
					$G_PGLOB->smarty->assign('error_msg', '参数错误，请审核后再操作');
					$G_PGLOB->smarty->display('comm/error.tpl');
            }
        }
    }

    /**
     * 商品单位列表
     * @author kangronghua
     * 
     */
    private function goodsUnitList() {
        global $G_PGLOB;
        
        $G_PGLOB->smarty->assign('edit_permission', $G_PGLOB->has_permission(506));
        $G_PGLOB->smarty->display('goods/unit_list.tpl');
    }
    
    /**
     * 单位添加 
     *
     */
    private function unitAdd() {
    	global $G_PGLOB;
    	
    	//得到最大的单位排序
    	$maxSortOrder = $G_PGLOB->DBCA->stmt_query('SELECT IF(MAX(sortOrder) IS NULL ,1 , MAX(sortOrder)+1) AS max_sortOrder FROM goods_unit') ;
		
    	//统一传参数组
    	$unitInfo = new entity('goods_unit') ;
    	$unitInfo->sortOrder = $maxSortOrder[0]->max_sortOrder ;
    	$unitInfo->useFlg    = 1 ;
    	
    	$G_PGLOB->smarty->assign('unitInfo' , $unitInfo);
    	$G_PGLOB->smarty->display('goods/unit_edit.tpl');
    }
    
    /**
     * 单位编辑 
     *
     */
    private function unitEdit() {
    	global $G_PGLOB;
    	
    	//得到单位信息
    	$unitID   = $_REQUEST['unitID'] ;
    	$unitInfo = new entity('goods_unit') ;
    	$unitInfo->unitID = $unitID ;
    	if(!$G_PGLOB->DBCA->fetch( $unitInfo )){
    		$G_PGLOB->smarty->assign('error_msg', sprintf('单位号「%d」,的单位不存在，请审核后再操作' , $unitID));
			$G_PGLOB->smarty->display('comm/error.tpl');
			die() ;
    	}
       	$G_PGLOB->smarty->assign('unitInfo' , $unitInfo);
    	$G_PGLOB->smarty->display('goods/unit_edit.tpl');
    } 
    
}